﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using GC2.Bussines;
using GC2.Bussines.BMAdmin;
using GC2.Entities.Admin;
using GC2.Entities.Seguridad;
using GC2.ExceptionHandler;

public partial class Clientes_ClientesAdmin : GenericPage
{
    #region Propiedades
    Usuarios usuario = new Usuarios();
    Clientes clienteActual = new Clientes();    
    List<Clientes> listClientes = new List<Clientes>();
    private ExceptionConstructor exNew = new ExceptionConstructor();
    #endregion

    #region Controles

    protected void Page_Load(object sender, EventArgs e)
    {
        SetearCultura();
        SetearConexion();
        if (!IsPostBack)
        {
            usuario = (Usuarios)Session["UsuarioActual"];
        }
    }


    protected void btnBuscar_Click(object sender, EventArgs e)
    {
        try
        {
            BMClientes bmClientes = new BMClientes(Connection);

            if (txtCodigo.Text.Trim() != "")
            {
                clienteActual.Id_Cliente = Convert.ToInt16(txtCodigo.Text.Trim());
            }
            else
            {
                clienteActual.Id_Cliente = 0;
            }

            clienteActual.Nombre = txtNombre.Text.Trim();

            listClientes = bmClientes.BMClientesSearch(clienteActual);

            Session["listClientes"] = listClientes;
            CargarGrilla();

        }
        catch (Exception ex) { ErrorUI(ex); }
    }

    protected void btnNuevo_Click(object sender, EventArgs e)
    {
        Session["Accion"] = 1;
        Response.Redirect("ClientesGestion.aspx", false);
    }

    protected void btnCancelar_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Seguridad/Default.aspx", false);
    }

    protected void gvClientes_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvClientes.PageIndex = e.NewPageIndex;
        CargarGrilla();
    }

    protected void gvClientes_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

    }

    protected void gvClientes_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            BMClientes bmClientes = new BMClientes(Connection);
            Clientes cliente = new Clientes();
            cliente.Id_Cliente = Convert.ToInt64(gvClientes.SelectedDataKey.Value);
            clienteActual = cliente;
            cliente = bmClientes.BMClientesGetOne(clienteActual);

            Session["Cliente"] = cliente;
            Session["Accion"] = 2;
            Response.Redirect("ClientesGestion.aspx", false);
        }
        catch (Exception ex) { ErrorUI(ex); }
    }

    #endregion

    #region Métodos
    private void CargarGrilla()
    {
        try
        {
            listClientes = (List<Clientes>)Session["listClientes"];
            gvClientes.DataSource = listClientes;
            gvClientes.DataBind();
        }
        catch (Exception ex) { ErrorUI(ex); }
    }

    #endregion

}
